package cn.ibobo.lease.admin.controller;

import cn.ibobo.lease.admin.service.impl.LeaseResDetailServiceImpl;
import cn.ibobo.lease.common.dto.LeaseResDetailDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.ListCopyToVOUtil;
import cn.ibobo.lease.common.util.PaginationVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.vo.LeaseResDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 租赁物品详情表控制器
 * @Author lililiang
 * @Date 2018-12-23 10:10:30
 */

@RestController
@Api(tags = "租赁物品详情表控制器")
@RequestMapping("leaseResDetail")
public class LeaseResDetailController{

    private static final Log LOGGER = LogFactory.getLog(LeaseResDetailController.class);

    @Autowired
    private LeaseResDetailServiceImpl service;
    @Autowired
    private ResultVOUtil<Object> resultObject;

    /**
	 * 租赁物品详情表列表
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
	 * @Param
	 * @Return
	 */
//    @ApiOperation(value = "租赁物品详情表列表", response = LeaseResDetailVO.class)
//    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            LeaseResDetailVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            LeaseResDetailDTO entityDTO = new LeaseResDetailDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<LeaseResDetailDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, LeaseResDetailVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 租赁物品详情表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @ApiOperation(value = "租赁物品详情表列表（无分页）", response = LeaseResDetailVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(
            @ApiParam(value = "租赁品详情编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "租赁品id",required = false) @RequestParam(required = false) String lid,
            @ApiParam(value = "0 下架  1 可用  2 在租",required = false) @RequestParam(required = false) Integer status,
            @ApiParam(value = "商家id",required = false) @RequestParam(required = false) String bid,
            @ApiParam(value = "所属区域id",required = false) @RequestParam(required = false) String areaid
    ){
        try {
            LeaseResDetailDTO entityDTO = new LeaseResDetailDTO();
            entityDTO.setCode(code);
            entityDTO.setLid(lid);
            entityDTO.setStatus(status);
            entityDTO.setBid(bid);
            entityDTO.setAreaid(areaid);
            List<LeaseResDetailDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, LeaseResDetailVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 租赁物品详情表详细信息
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
//    @ApiOperation(value = "租赁物品详情表详细信息", response = LeaseResDetailVO.class)
//    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            LeaseResDetailDTO entityDTO = service.queryOneByPk(id);
            LeaseResDetailVO entity = new LeaseResDetailVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存租赁物品详情表信息
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存租赁物品详情表信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(
            @ApiParam(value = "租赁品详情id,修改时必传",required = false) @RequestParam(required = false) String did,
            @ApiParam(value = "租赁品详情编号",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "租赁品id",required = false) @RequestParam(required = false) String lid,
            @ApiParam(value = "状态，0 下架  1 可用  -1 删除  2 在租",required = false) @RequestParam(required = false) Integer status,
            @ApiParam(value = "商家id",required = false) @RequestParam(required = false) String bid
    ) {
        try {
            LeaseResDetailDTO entityDTO = new LeaseResDetailDTO();
            entityDTO.setDid(did);
            entityDTO.setCode(code);
            entityDTO.setLid(lid);
            entityDTO.setBid(bid);
            entityDTO.setStatus(status);
            Integer save = service.save(entityDTO);
            return resultObject.error(save, ResultEnum.getMsg(save));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除租赁物品详情表信息
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除租赁物品详情表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "具体租赁物品did",required = true) @RequestParam(value = "did") String did){
        try {
            if(service.delete(did) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }


}